package org.msfj.admin.data.controller;

import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.admin.data.service.FormFieldCategoryService;
import org.msfj.admin.model.FormFieldCategory;
import org.msfj.admin.vo.FormFieldCategoryVo;
import org.msfj.common.base.BaseController;
import org.msfj.common.model.CascaderResult;
import org.msfj.common.model.CommonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author yangyan
 */
@Api(value = "自定义表单域分类接口", tags = "自定义表单域分类接口")
@RestController
@RequestMapping("data/formFieldCategory")
public class FormFieldCategoryController extends BaseController {

    @Autowired
    private FormFieldCategoryService formFieldCategoryService;

    /**
     * 添加自定义表单域
     *
     * @param formFieldCategoryVo
     * @return CommonResult.ok(FormFieldCategoryVo)
     */
    @PostMapping("/add")
    @ResponseBody
    public CommonResult addFormFieldCategory(FormFieldCategoryVo formFieldCategoryVo) {
        FormFieldCategory formFieldCategory = formFieldCategoryService.saveOrEdit(formFieldCategoryVo);
        return CommonResult.ok(formFieldCategoryService.setOtherProperties(formFieldCategory));
    }

    /**
     * 修改表单域分类
     *
     * @param formFieldCategoryVo
     * @return CommonResult.ok(FormFieldCategoryVo)
     */
    @PostMapping("/edit")
    @ResponseBody
    public CommonResult editFormFieldCategory(FormFieldCategoryVo formFieldCategoryVo) {
        FormFieldCategory formField = formFieldCategoryService.saveOrEdit(formFieldCategoryVo);
        return CommonResult.ok(formFieldCategoryService.setOtherProperties(formField));
    }

    /**
     * 逻辑删除表单域分类
     *
     * @param formFieldCategoryVo
     * @return CommonResult.ok(FormFieldCategoryVo)
     */
    @PostMapping("/del")
    @ResponseBody
    public CommonResult delFormFieldCategory(FormFieldCategoryVo formFieldCategoryVo) {
        FormFieldCategory formField = formFieldCategoryService.delById(formFieldCategoryVo);
        return CommonResult.ok(formFieldCategoryService.setOtherProperties(formField));
    }

    /**
     * 逻辑删除系统用户信息
     *
     * @param ids
     * @return CommonResult.ok()
     */
    @PostMapping("/dels")
    @ResponseBody
    public CommonResult dels(@RequestParam(value = "ids[]", required = false) List<String> ids) {
        FormFieldCategory formField = new FormFieldCategory();
        formFieldCategoryService.delByIds(ids, formField);
        return CommonResult.ok();
    }

    /**
     * 真实删除表单域分类
     *
     * @param formFieldCategoryVo
     * @return CommonResult.ok()
     */
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult deleteFormFieldCategory(FormFieldCategoryVo formFieldCategoryVo) {
        formFieldCategoryService.trueDelById(formFieldCategoryVo);
        return CommonResult.ok();
    }

    /**
     * 根据id查找表单域分类
     *
     * @param formFieldCategoryVo
     * @return CommonResult.ok(FormFieldCategoryVo)
     */
    @GetMapping("/get")
    @ResponseBody
    public CommonResult getFormFieldCategory(FormFieldCategoryVo formFieldCategoryVo) {
        FormFieldCategory formField = formFieldCategoryService.getById(formFieldCategoryVo.getId());
        return CommonResult.ok(formFieldCategoryService.setOtherProperties(formField));
    }

    /**
     * 根据搜索参数查找所有表单域分类
     *
     * @return CommonResult.ok(List < FormFieldCategoryVo >)
     */
    @GetMapping("/getTree")
    @ResponseBody
    public CommonResult getTree() {
        List<FormFieldCategoryVo> formFieldCategoryVoList = formFieldCategoryService.getTree();
        return CommonResult.ok(formFieldCategoryVoList);
    }


    @RequestMapping("/getCascader")
    @ResponseBody
    public CommonResult getCascader(String id) {
        List<FormFieldCategoryVo> formFieldCategoryVoList = formFieldCategoryService.getTree();
        List<CascaderResult> cascaderResults = CascaderResult.getCascaderResult("Id", "Name","TreePosition" , id, formFieldCategoryVoList);
        return CommonResult.ok(cascaderResults);
    }
}
